package com.cskt.petpms.service.impl;

import com.cskt.common.condition.SearchProductCondition;
import com.cskt.common.constants.Constants;
import com.cskt.common.entity.pojo.Sku;
import com.cskt.common.entity.vo.*;
import com.cskt.common.utils.AliyunOssUtil;
import com.cskt.common.utils.RedisKeyUtils;
import com.cskt.petpms.mapper.ProductCategoryMapper;
import com.cskt.petpms.mapper.SkuMapper;
import com.cskt.petpms.mapper.SpuMapper;
import com.cskt.petpms.service.ProductService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 商品servcie层实现
 *
 * @author admin
 **/
@Service
public class ProductServiceImpl implements ProductService {

    private static final Logger log = LoggerFactory.getLogger(ProductServiceImpl.class);

    @Resource
    private SpuMapper spuMapper;
    @Resource
    private SkuMapper skuMapper;
    @Resource
    private ProductCategoryMapper categoryMapper;
    
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageVO<ProductVO> getProductVOPage(SearchProductCondition condition) {
        //先查数量
        Integer total = spuMapper.countProductVOByCondition(condition);
        List<ProductVO> productVOS = spuMapper.selectProductVOByCondition(condition);
        //处理商品图片
        productVOS.forEach(
                productVO -> {
                    String url = AliyunOssUtil.getSymlink(productVO.getDefaultPicUrl());
                    String key = RedisKeyUtils.generateFileRedisKey(productVO.getDefaultPicUrl());
                    //将其存入redis
                    stringRedisTemplate.opsForValue().set(key, url, Constants.Time.ALI_OSS_REDIS_PATH_TIME, TimeUnit.SECONDS);
                    productVO.setDefaultPicUrl(url);
                }
        );
        PageVO<ProductVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(condition, pageVO);
        pageVO.setTotal(total);
        pageVO.setList(productVOS);
        return pageVO;
    }

    @Override
    public ProductDetailVO getProductDetailById(Long id) {
        SpuVO spuVO = spuMapper.selectSpuVOById(id);
        if (ObjectUtils.isEmpty(spuVO)) {
            return null;
        }
        //处理spu中商品详情图片的路径
        spuVO.getProductDetailPicList().forEach(
                spuPicVO -> spuPicVO.setPicUrl(AliyunOssUtil.getSymlink(spuPicVO.getPicUrl()))
        );
        //获取category集合
        List<CategoryVO> categories = categoryMapper.selectProductCategoryVO().stream()
                .map(productCategoryVO -> {
                    CategoryVO categoryVO = new CategoryVO();
                    BeanUtils.copyProperties(productCategoryVO, categoryVO);
                    return categoryVO;
                }).toList();
        //获取SkuVO集合
        List<SkuVO> skuList = skuMapper.selectSkuVOBySpuId(spuVO.getId());
        for (SkuVO skuVO : skuList) {
            for (SkuPicVO skuPicVO : skuVO.getSkuPicList()) {
                skuPicVO.setPicUrl(AliyunOssUtil.getSymlink(skuPicVO.getPicUrl()));
            }
        }
        ProductDetailVO productDetailVO = new ProductDetailVO();
        productDetailVO.setSpu(spuVO);
        productDetailVO.setCategories(categories);
        productDetailVO.setSkuList(skuList);
        return productDetailVO;
    }

    @Override
    public Sku getSkuBySkuId(Long skuId) {
        return skuMapper.selectByPrimaryKey(skuId);
    }
}
